<?php


namespace App\Exports;

use App\Models\PhotoCatalog;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

/**
 * Class BookTitleExport29
 * @package App\Exports
 * 29号案卷目录导出格式
 */
class PhotoCatalogExport implements  FromCollection, WithTitle, WithColumnWidths, WithStyles,  WithCustomStartCell
{
    protected $photoArchivesId; //项目分类
    protected $totalRow; //总行数

    public function __construct()
    {
        $this->photoArchivesId = request()->input('photo_archives_id', 0);
    }

    /**
     * 获取数据集合
     */
    public function collection()
    {
        $list = PhotoCatalog::query()
            ->where('photo_archives_id', $this->photoArchivesId)
            ->get();

        if ($list->isEmpty()) return collect([]);

        $list = $list->toArray();
        $r_list = [];
        foreach ($list as $val) {
            $arr['serial_no']     = $val['serial_no'];  //照片号
            $arr['title']         = htmlspecialchars_decode($val['title']); //标题
            $arr['photo_date']    = $val['photo_date']; //拍摄日期
            $arr['remark']        = $val['remark'];     //备注
            $r_list[] = $arr;
        }

        $this->totalRow = count($r_list) + 10;

        return collect($r_list);
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return date('m月d日H时i分').'导出的照片卷内目录';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 16, 'B' => 80, 'C' => 20, 'D' => 16
        ];
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A3';
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(32); //设置行高(全部行高)
        $sheet->getStyle('A1:D'.$this->totalRow)->getFont()->setName('宋体'); //宋体

        $sheet->mergeCells('A1:D1');
        $sheet->setCellValue('A1', '照 片 档 案 卷 内 目 录');
        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '照片号');
        $sheet->setCellValue('B2', '题 名');
        $sheet->setCellValue('C2', '拍摄时间');
        $sheet->setCellValue('D2', '备 注');

        $sheet->getStyle('A1:D'.$this->totalRow)->getAlignment()->setHorizontal('center');//水平居中
        $sheet->getStyle('B3:B'.$this->totalRow)->getAlignment()->setHorizontal('left');//水平靠左
        $sheet->getStyle('B3:B'.$this->totalRow)->getAlignment()->setWrapText(true); //自动换行
        $sheet->getStyle('A1:D'.$this->totalRow)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1')->getFont()->setSize(18);//字体大小
        $sheet->getStyle('A1')->getFont()->setBold(true);//粗体
        $sheet->getStyle('A2:D2')->getFont()->setSize(14);//字体大小

    }

}
